package com.logreview.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ShellUtil {
    private static Logger logger = LoggerFactory.getLogger(ShellUtil.class);

    public static String extcute(String shell){
        Process process = null;

        StringBuffer sb = new StringBuffer();
        BufferedReader input = null;
        try {
            process = Runtime.getRuntime().exec(shell);
            input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                sb.append(line);
            }
            logger.info("shell execeute:{},result:{}",shell,sb.toString());
        } catch (IOException e) {
            logger.error("shell execeute failed",e);
            return  e.getMessage();
        }finally {
            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return  sb.toString();
    }
}
